home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / yg600w95.zip / YGREPTST.PAS < prev   
Pascal/Delphi Source File  |  1996-10-06  |  6KB  |  219 lines

  1. {******************************************************************}
  2. { Description:                                                     }
  3. {   This is a small sample program to demonstrate how to call some }
  4. {   of the functions in the YGREP32.DLL from Borland's Delphi 2.   }
  5. {   You need the YGrepInt.pas and test.txt file for this to work.  }
  6. {   Test.txt is only used to test the SFile Search functions.      }
  7. {                                                                  }
  8. { Author:                                                          }
  9. {   Damien Lewis (louie@melbpc.org.au) - 6th August 1996           }
  10. {******************************************************************}
  11.  
  12. unit YgrepTst;
  13.  
  14. interface
  15.  
  16. uses
  17.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  18.   StdCtrls, ExtCtrls;
  19.  
  20. type
  21.   TForm1 = class(TForm)
  22.     btnTest: TButton;
  23.     lblSearchStr: TLabel;
  24.     rgrpTest: TRadioGroup;
  25.     lblTextStr: TLabel;
  26.     lblReplaceStr: TLabel;
  27.     lblSearchResult: TLabel;
  28.     btnExit: TButton;
  29.     Label1: TLabel;
  30.     Label2: TLabel;
  31.     Label3: TLabel;
  32.     lblReplaceResult: TLabel;
  33.     cboxMatchCase: TCheckBox;
  34.     procedure btnTestClick(Sender: TObject);
  35.     procedure btnExitClick(Sender: TObject);
  36.     procedure rgrpTestClick(Sender: TObject);
  37.     procedure FormShow(Sender: TObject);
  38.   private
  39.     { Private declarations }
  40.   public
  41.     { Public declarations }
  42.   end;
  43.  
  44. var
  45.   Form1: TForm1;
  46.  
  47. implementation
  48.  
  49. uses
  50.   YGrepInt;    { This is the interface to the YGREP32.DLL }
  51.  
  52. var
  53.   { Define a few global variables }
  54.   SearchStr: array[0..50] of char;
  55.   ReplaceStr: array[0..50] of char;
  56.   TextStr: array[0..50] of char;
  57.   SearchResult: array[0..50] of char;
  58.   ReplaceResult: array[0..50] of char;
  59.  
  60. {$R *.DFM}
  61.  
  62. procedure TForm1.FormShow(Sender: TObject);
  63. begin
  64.   Form1.rgrpTestClick(Form1);
  65. end;
  66.  
  67. procedure TForm1.btnTestClick(Sender: TObject);
  68. var
  69.   { Define some local variables }
  70.   MatchCase: Integer;
  71.   NoOfErrors: Integer;
  72.   Result: Integer;
  73.   LinesFound: LongInt;
  74.   TempStr: String;
  75.   ResultStr: String;
  76.   ReplaceString: String;
  77.   SearchFile: String;
  78.   pGI: PtagRGrepInfo;   { Pointer to the RGrepInfo Structure }
  79. begin
  80.   New(pGI);             { Create a new structure and assign pointer to it }
  81.  
  82.   if cboxMatchCase.Checked = True then
  83.     MatchCase := 1
  84.   else
  85.     MatchCase := 0;
  86.  
  87.   case rgrpTest.ItemIndex of
  88.     0:
  89.     { SAGrep Search Functions }
  90.     begin
  91.       NoOfErrors := 2;
  92.       Result := SCompileAGrep(SearchStr, NoOfErrors, MatchCase);
  93.       Result := SAGrep(TextStr);
  94.       if Result > 0 then
  95.         lblSearchResult.Caption := 'Match Found';
  96.       if Result = 0 then
  97.         lblSearchResult.Caption := 'No Match';
  98.       if Result < 0 then
  99.         lblSearchResult.Caption := 'Error Occurred';
  100.     end;
  101.  
  102.     1:
  103.     { SRGrep Search Functions }
  104.     begin
  105.       Result := SCompileRGrep(SearchStr, MatchCase);
  106.       Result := SRGrep(TextStr);
  107.       if Result > 0 then
  108.         lblSearchResult.Caption := 'Match Found';
  109.       if Result = 0 then
  110.         lblSearchResult.Caption := 'No Match';
  111.       if Result < 0 then
  112.         lblSearchResult.Caption := 'Error Occurred';
  113.     end;
  114.  
  115.     2:
  116.     { SRGrep Replace Functions }
  117.     begin
  118.       Result := SCompileRGrep(SearchStr, MatchCase);
  119.       Result := SRGrep(TextStr);
  120.       Result := SRGrepSubsBuild(SearchStr, ReplaceResult, 50);
  121.       Result := SRGrepSubstitute(TextStr, ReplaceStr, ReplaceResult, 50);
  122.       lblSearchResult.Caption := ReplaceResult;
  123.     end;
  124.  
  125.     3:
  126.     { SRGrep File Search Functions }
  127.     begin
  128.       SearchFile := ExtractFilePath(Application.ExeName) + 'test.txt';
  129.       if FileExists(SearchFile) then begin
  130.         SFileSetFlags(0, 0, 1, 0, 0);
  131.         Result := SCompileRGrep(SearchStr, MatchCase);
  132.         Result := SFileOpen(PChar(SearchFile));
  133.         repeat
  134.           TempStr := ResultStr;
  135.           ResultStr := SFileRGrep;
  136.         until ResultStr = '';
  137.         LinesFound := SFileClose;
  138.         lblSearchResult.Caption := IntToStr(LinesFound) + ' Lines Found';
  139.         lblReplaceResult.Caption := 'Line: ' + TempStr;
  140.       end else begin
  141.         lblSearchResult.Caption := 'ERROR: Test file was not found!';
  142.       end;
  143.     end;
  144.  
  145.     4:
  146.     { RGrep Search Functions }
  147.     begin
  148.       Result := CompileRGrep(SearchStr, MatchCase, pGI);
  149.       Result := RGrep(TextStr, pGI);
  150.       if Result > 0 then
  151.         lblSearchResult.Caption := 'Match Found';
  152.       if Result = 0 then
  153.         lblSearchResult.Caption := 'No Match';
  154.       if Result < 0 then
  155.         lblSearchResult.Caption := 'Error Occurred';
  156.     end;
  157.   end;
  158.   Dispose(pGI);         { Dstroy structure when finished }
  159. end;
  160.  
  161. procedure TForm1.btnExitClick(Sender: TObject);
  162. begin
  163.   Close;
  164. end;
  165.  
  166. procedure TForm1.rgrpTestClick(Sender: TObject);
  167. begin
  168.   SearchResult := '';
  169.   ReplaceResult := '                                        ';
  170.   case rgrpTest.ItemIndex of
  171.     0:
  172.     { Fill variables for SAGrep Search Functions }
  173.     begin
  174.       SearchStr := 'East Germany';
  175.       ReplaceStr := '';
  176.       TextStr := 'West Germany';
  177.     end;
  178.  
  179.     1:
  180.     { Fill variables for SRGrep Search Functions }
  181.     begin
  182.       SearchStr := 'Horse';
  183.       ReplaceStr := '';
  184.       TextStr := 'A horse! My kingdom for a horse!';
  185.     end;
  186.  
  187.     2:
  188.     { Fill variables for SRGrep Replace Functions }
  189.     begin
  190.       SearchStr := 'Horse';
  191.       ReplaceStr := 'donkey';
  192.       TextStr := 'A horse! My kingdom for a horse!';
  193.     end;
  194.  
  195.     3:
  196.     { Fill variables for SRGrep File Search Functions }
  197.     begin
  198.       SearchStr := 'Apples';
  199.       ReplaceStr := '';
  200.       TextStr := 'test.txt';
  201.     end;
  202.  
  203.     4:
  204.     { Fill variables for RGrep Search Functions }
  205.     begin
  206.       SearchStr := 'Horse';
  207.       ReplaceStr := '';
  208.       TextStr := 'A horse! My kingdom for a horse!';
  209.     end;
  210.   end;
  211.   lblSearchStr.Caption := SearchStr;
  212.   lblReplaceStr.Caption := ReplaceStr;
  213.   lblTextStr.Caption := TextStr;
  214.   lblSearchResult.Caption := SearchResult;
  215.   lblReplaceResult.Caption := ReplaceResult;
  216. end;
  217.  
  218. end.
  219.